#!/usr/bin/php
<?php
/*
 * CSV2JSON PHP script
 * This script converts a CSV file to a JSON object. It expects all values to
 * be enclosed in double quotes (") and to be seperated by commas (,)
 * 
 * Written by Hay Kranen < http://www.haykranen.nl > < hay at bykr dot org > 
 *
 * USE
 * Make this executable
 * $ chmod u+x csv2json
 * Then invoke it from the command line like this
 * $ csv2json myfile.csv
 * The script will automatically write the resulting file as myfile.json
 */

function quit($msg) {
    die($msg . "\n");
}

// For PHP < 5.3
if (!function_exists('str_getcsv')) {
    function str_getcsv($input, $delimiter = ",", $enclosure = '"', $escape = "\\") {
        $fiveMBs = 5 * 1024 * 1024;
        $fp = fopen("php://temp/maxmemory:$fiveMBs", 'r+');
        fputs($fp, $input);
        rewind($fp);

        $data = fgetcsv($fp, 1000, $delimiter, $enclosure); //  $escape only got added in 5.3.0

        fclose($fp);
        return $data;
    }
}

$filename = $argv[1];
if (!file_exists($filename)) quit("File does not exist: $filename");

$file = file($filename);
if (!$file) quit("File read error with $filanme");

echo "Processing $filename... \n";

$data = array();

// Get the first line for the values
$hash = str_getcsv($file[0]);
unset($file[0]);

foreach ($file as $line) {
    // Create new array for the line
    $values = str_getcsv($line);
    $obj = array();
    foreach ($values as $index => $value) {
        $key = $hash[$index];
        $obj[$key] = $value;
    }
    $data[] = $obj;
}

echo "Ready..now writing to JSON\n";

$json = json_encode($data);
if (!$json) quit("Could not encode to JSON");

// Create a filename based on the input file
$out = pathinfo($filename, PATHINFO_FILENAME) . ".json";
if(file_exists($out)) quit("File already exists! Aborting!");
if(!file_put_contents($out, $json)) quit("Could not write contents to $out");

quit("Written JSON data to $out");
